import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -04 -11
 * Time: 15:45
 */
public class Triangle2D {
    private MyPoint2D p1;
    private MyPoint2D p2;
    private MyPoint2D p3;
    public Triangle2D(){
        this.p1 = new MyPoint2D(0,0);
        this.p2 = new MyPoint2D(1,1);
        this.p3 = new MyPoint2D(2,5);
    }
    public Triangle2D(MyPoint2D p1, MyPoint2D p2, MyPoint2D p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public MyPoint2D getP1() {
        return p1;
    }

    public void setP1(MyPoint2D p1) {
        this.p1 = p1;
    }

    public MyPoint2D getP2() {
        return p2;
    }

    public void setP2(MyPoint2D p2) {
        this.p2 = p2;
    }

    public MyPoint2D getP3() {
        return p3;
    }

    public void setP3(MyPoint2D p3) {
        this.p3 = p3;
    }

    //返回三角形面积
    public double getArea(){
        return 0.5 * Math.abs(p1.getX() * (p2.getY() - p3.getY()) + p2.getX() * (p3.getY() - p1.getY()) + p3.getX() * (p1.getY() - p2.getY()));
    }
    public double getArea(MyPoint2D p1,MyPoint2D p2,MyPoint2D p3){
        return 0.5 * Math.abs(p1.getX() * (p2.getY() - p3.getY()) + p2.getX() * (p3.getY() - p1.getY()) + p3.getX() * (p1.getY() - p2.getY()));
    }
    //返回三角形周长
    public double getPerimeter(){
        return p1.getLength()+p2.getLength()+p3.getLength();
    }
    //给定点p如果在当前三角形内返回true
    public boolean contains(MyPoint2D p){
        double area1 = getArea(p1, p2, p);
        double area2 = getArea(p1, p, p3);
        double area3 = getArea(p, p2, p3);

        return area1 + area2 + area3 == getArea();

    }
    //给定三角形t如果在当前三角形内返回true
    public boolean contains(Triangle2D  t){
        return contains(t.getP1()) && contains(t.getP2()) && contains(t.getP3());
    }
    //给定三角形t如果与当前三角形有重叠部分返回true
    public boolean overlaps(Triangle2D  t){
        if (contains(t)) return false;

        if (t.contains(p1) || t.contains(p2) || t.contains(p3)) return true;

        Line[] sides = {new Line(p1, p2), new Line(p1, p3), new Line(p3, p2)};
        Line[] tSides = {new Line(t.getP1(), t.getP2()), new Line(t.getP1(), t.getP3()), new Line(t.getP3(), t.getP2())};

        for (int i=0; i<sides.length; i++) {
            for (int j=0; j<tSides.length; j++) {
                if (sides[i].intersectionLine(tSides[j])) return true;
            }
        }

        return false;
    }

}
